package com.free.show;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class SliderExample
{
    
    Display dispaly;
    
    Shell shell;
    
    SliderExample()
    {
        dispaly = new Display();
        shell = new Shell(dispaly);
        shell.setSize(300, 250);
        shell.setText("A Slider Example");
        // 添加Slider对象
        final Slider slide = new Slider(shell, SWT.V_SCROLL);
        // 设置Slider的位置和大小
        slide.setBounds(170, 25, 25, 20);
        // 设置Slider的最小值
        slide.setMinimum(0);
        // 设置Slider的最大值
        slide.setMaximum(100);
        // 设置Slider单击左右箭头的增加或递减值
        slide.setIncrement(1);
        
        final Text t = new Text(shell, SWT.BORDER);
        t.setBounds(115, 25, 55, 20);
        t.setText("0");
        t.setFocus();
        
        // 添加Slider的事件监听器
        slide.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent e)
            {
                t.setText(new Integer(slide.getSelection()).toString());
            } // getSelection能得到的最大值并不是getMaximum()的值，要减去getThumb()值，后者是中间滑块所拥有的值，
              // 缺省为10，最小为1
        });
        
        shell.open();
        while (!shell.isDisposed())
        {
            if (!dispaly.readAndDispatch())
                dispaly.sleep();
        }
        dispaly.dispose();
    }
    
    public static void main(String[] args)
    {
        new SliderExample();
    }
    
}
